package fileIO;

import java.io.*;
import skin.Skin;
import tools.StringTools;

public class Options {
	public Skin skin;
	private String skinPath;
	
	//TODO: Consider if this really is the best way to handle this.
	//Not sure they belong in options and not in the skin...or the database
	public String GuildmasterPortraitPathMale;
	public String GuildmasterPortraitPathFemale; 
	
	public Options()
	{
		
		try {
			BufferedReader reader = new BufferedReader(new FileReader("config/options.cfg")); //this file name i'm afraid we'll have to keep hardcoded....gotta start somewehre
			String curLine; // this will be where the current line will be stored
			while((curLine = reader.readLine()) != null) //while there's something left of the config, keep reading it
			{
				// ** seems like a good idea to me, if you had plans for empty lines, feel free to remove
				curLine = StringTools.clearComments(curLine).trim();	// drop comments & trim
				if (curLine.isEmpty()) continue;						// skip empty lines
				
				
				enforce(curLine); //this actually changes the Options object
			}
			reader.close();
		} catch (FileNotFoundException e) {
			//TODO: insert appropriate new config file generation here
			e.printStackTrace();
		} catch (IOException e) {
			//TODO: WTF causes an IO exception anyway? corrupted file? I guess here we regen config from a backup or soemthing
			e.printStackTrace();
		}
		//Hereafter run other things that might want to deal w/IO. Having only 1 open stram at a time is a good idea
		
		if (skinPath == null) skin = new SkinIO().buildSkin(); 	// if skin file is not defined, build default skin
		else skin = new SkinIO().buildSkin(skinPath);			// else build skin from file
	}
	private void enforce(String rawCfg)
	{
		//TODO: make this a switch
		String[] parsedCfg = FileUtil.parse(rawCfg); //0 will always have the config name, everything after, the vals
		switch(parsedCfg[0])
		{
		case "skin":
			skinPath = ("config/".concat(parsedCfg[1]).concat(".skin"));
			break;
		//TODO: portrait paths enforcement?
		case "guildmasterportraitpathmale":
			GuildmasterPortraitPathMale = parsedCfg[1];
			break;
		case "guildmasterportraitpathfemale":
			GuildmasterPortraitPathFemale = parsedCfg[1];
			break;
		default:
			System.err.println("Unknown config argument '" + parsedCfg[0] + "'");	
		}
	}

}
